define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            var h=parent.window.location.toString();
            var ids= h.substring(h.indexOf("/ids/")+5,h.indexOf("?"));
            Table.api.init({
                extend: {
                    index_url: 'sensor/index',
                    add_url: 'sensor/add?ids='+ids,
                    edit_url: 'sensor/edit',
                    del_url: 'sensor/del',
                    multi_url: 'sensor/multi',
                    table: 'sstalink_sensor'
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: window.location,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: '传感器序号'},
                        {field: 'name', title: __('Name')},
                        {field: 'deviceid', title: __('Deviceid')},
                        {field: 'datatype', title: __('Datatype'),formatter: function(value,row,index){
                            switch (value){
                                case 0:     return "数值型";
                                case 1:     return "文本型";
                                case 2:     return "gps型";
                                case 3:     return "图片型";
                                case 4:     return "开关型";
                                case 5:     return "自定义";
                            }
                        }},
                        {field: 'updatetime', title: __('Updatetime')},
                        {field: 'createtime', title: __('Createtime')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate},
                        {field: 'datatype', title: __('Data'),table: table,formatter:function (value,row,index) {
                            var url="";
                            var patt=new RegExp("[0-9a-z]{9}");
                            // document.write(patt1.exec("温室Test(08ac92f63)"));
                            switch (value){
                                case 0: url="data/number?id="+row.id+"&device_id="+patt.exec(row.deviceid);break;
                                case 1: url="data/text?id="+row.id+"&device_id="+patt.exec(row.deviceid);break;
                                case 2: url="data/gps?id="+row.id+"&device_id="+patt.exec(row.deviceid);break;
                                case 3: url="data/image?id="+row.id+"&device_id="+patt.exec(row.deviceid);break;
                                case 4: url="data/boolean?id="+row.id+"&device_id="+patt.exec(row.deviceid);break;
                            }
                            return "<a href='"+url+"' class='fa fa-list btn btn-xs btn-primary btn-dialog'> 详细数据</a>";
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            init();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $(document).on("click", ".fieldlist .append", function () {
                    var rel = parseInt($(this).closest("dl").attr("rel")) + 1;
                    $(this).closest("dl").attr("rel", rel);
                    $('<dd><div></div><input type="text" name="hook[' + rel + ']" class="form-control" id="value-' + rel + '" value="" size="40" /> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>').insertBefore($(this).parent());
                });
                $(document).on("click", ".fieldlist dd .btn-remove", function () {
                    $(this).parent().remove();
                });
            }

        }
    };
    return Controller;
});
